package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Member;
import com.itheima.domain.Order;
import com.itheima.domain.OrderSetting;
import com.itheima.entity.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: OrderServiceImpl
 * @Author: Solomon
 * @Date: 2020/7/13 14:08
 * @Description:
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    //移动端进行预约设置
    @Override
    public Result submit(Map map) throws Exception {
        //1. 判断当前日期是否进行了预约设置
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = orderSettingMapper.findOrderSettingByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting == null){
            //没有进行预约设置
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //进行了预约设置
        //2. 判断当前是否预约满了
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number){
            //预约满了
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //3. 当前用户是否进行重复预约(同一用户同一天同一个套餐)
        //根据用户输入的手机号进行查询会员
        String telephone = (String) map.get("telephone");
        Member member = memberMapper.findByTelephone(telephone);

        if (member != null){
            //判断当前用户是否重复预约
            Integer memberId = member.getId();//用户id
            //Date orderDate = DateUtils.parseString2Date(orderDate);   预约日期
            Integer setmealId = (Integer) map.get("setmealId");//套餐id
            //封装查询条件
            Order order = new Order(memberId,DateUtils.parseString2Date(orderDate),setmealId);
            //查询当前用户是否预约,该方法是一个通用的方法，可以查询多种条件
            // 所以返回的结果可能是多个，所以使用集合接收
            List<Order> orderList = orderMapper.findByCondition(order);

            if (orderList != null && orderList.size() > 0){
                //已经进行了预约，重复预约了，返回预约失败信息
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            //该手机号的用户不是会员，先进行会员设置
            member = new Member();
            //设置member信息
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberMapper.addMember(member);
        }

        //5. 调用方法，进行预约，并更新预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员id
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐id
        //新增预约
        orderMapper.addOrder(order);
        //根据日期新增预约人数+1
        orderSettingMapper.updateReservations(DateUtils.parseString2Date(orderDate));

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //页面跳转到预约成功时，回显数据
    @Override
    public Map findById(Integer orderId) throws Exception {
        //体检人   体检套餐   体检日期   预约类型
        Map map = orderMapper.findById4Details(orderId);

        if (map != null){
            //日期格式处理
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
